<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Loading</title>
<style type="text/css">
body {
	width: 1920px;
	height: 1080px;
	padding: 0;
	margin: 0;
	background: #1E2028 no-repeat;
}	
.clogo {
	text-align: center;
	margin-top: 360px;
	margin-left:660px;
	width: 600px;
	font-size: 24px;
	color: white;
}

.msg {
	display: none;
}

</style>
</head>
<body>
	<div class="clogo">
		<br/>
		<br/>
		<img id="loadingpic" src="loading_blue.gif"/>
		<br/>
		<br/>
		<span id="updatelable"> ...</span>
	</div>
</body>
</html>

<script type="text/javascript" src="lang_n.js"></script>
<script type="text/javascript">
var pakobj = null;
var timeout_sec = 20;
var max_idletime = 5;
var updatelable = document.getElementById("updatelable");

function uni_onready(){
	setLoadingImage();//519设置背景加载图标

	var uiSettings = wae.create("UI.UISettings");
	uiSettings.set1080P();
 
 	pakobj = wae.create("WAE.AppPackage");  
 	setInterval( check_update, 1000 * 3 );     

 	setTimeout( check_server, 1000 * 15 );
}

function setLoadingImage(){
	//313注释 519打开
	// var themeStyle = localStorage.getItem("theme_style");
	// console.log("localStorage theme style: " + themeStyle);
	// if(!themeStyle || themeStyle == undefined){
	// 		themeStyle = "blue";
	// }
	// if ( themeStyle == "blue" ) {
	// 	themeStyle = "blue";
	// }else if( themeStyle == "yellow" ){
	// 	themeStyle = "yellow";
	// }else if( themeStyle == "pink" ){
	// 	themeStyle = "pink";
	// }

	var loadingpic = document.getElementById("loadingpic");
	//loadingpic.src = "loading_" +themeStyle+ ".gif"  //519
	loadingpic.src = "loading.gif"; //313
}

function openSetting() {
	var intent = wae.create("Intent");
	intent.startActivity( "com.android.settings","com.android.settings.Settings");
}

function check_server() {
	var SERVER_URL = waeparams.getContentProviderPersistence("mainhosturl");
	portalurl = SERVER_URL + "download/ZIP_CONFIG.xml";

	console.log("portalurl is " + portalurl );

	var hc_obj = wae.create("Net.HttpClient");
	var res = hc_obj.getWithSync( portalurl );

	if ( !! res ) {
		return;
	}
  //       var msg = TR( "Failed to get server response, please check with your network configuration" );
  //       var obj = {};
  //       obj.msg = msg;
  //       obj.ok = TR("Settings");
  //       obj.cancel = TR("Cancel");
  //       var r = confirm( JSON.stringify(obj) );

	// if ( ! r ) {
	// 	return;
	// }
	// openSetting();
}


function get_loading_label() {
	try {
		var lid = localStorage.getItem("tva_lang_id");
		if ( !!lid && lid.indexOf("ta") >= 0 ) {
			return  "Đang tải";
		}
	}catch(e){}
	return "Loading";
}
var loading_label = get_loading_label();

updatelable.innerHTML = loading_label + " ... ";


function check_update () {

 	if ( pakobj == null ) {
 		alert( "Error: fail to get app package info!" );
 		return;
 	}
	
 	if ( pakobj.getIdleTime() > 1000 * timeout_sec ) {
 		console.log("getIdleTime: " + pakobj.getIdleTime() ); 
 //		alert("connect server timeout!");
 		reload_page();
 	}

 	if ( !! pakobj.getUpdateError() ) {
 		console.log("getUpdateError: " + pakobj.getUpdateError() ); 
 //		alert("Error: " + pakobj.getUpdateError() ); 
 		reload_page();
 	}

	var idletime = Math.floor(get_idletime() / 1000 );
// var msgDiv = document.getElementById("msg");
	
	updatelable.innerHTML = loading_label + " [ " + pakobj.getUpdateRate() + "%]  ... " + idletime + " ";
}


var startup_time = sessionStorage.getItem("loading_startup_time");
if ( !! startup_time ) 
	startup_time = parseInt( startup_time );

if ( ! startup_time || isNaN(startup_time) ) {
	var myDate = new Date();
	startup_time = myDate.getTime();
	sessionStorage.setItem("loading_startup_time", startup_time );
} 

function get_idletime() {
	var check_time = (new Date()).getTime();
	var idletime = check_time - startup_time;
	return idletime;
}

// var c = Math.floor(get_idletime() / 1000 );
// var msgDiv = document.getElementById("msg");
// var msgText = msgDiv.innerHTML;
// for( var i=0; i<c; i++ ) {
// 	msgText += ".";
// }
// msgDiv.innerHTML = msgText;

function reload_page() {
	console.log("reload page");
	var idletime = get_idletime() / 1000 ;
	console.log("reload page: idletime: " + idletime);
	if ( idletime > max_idletime ) {
//		alert("網路連接失敗，確認進入設定！");
		// alert("Connect to Server Failed!\n\nClick OK to setup network!");


		// alert("Fatal, fail to load data in " + max_idletime + " seconds\n"
		// 		+"Please browser the local app to check network!\n");
				
		// var msg = TR( "Failed to get server response, please check with your network configuration" );
		// var obj = {};
		// obj.msg = msg;
		// obj.ok = TR("Settings");
		// obj.cancel = TR("Cancel");
		// var r = confirm( JSON.stringify(obj) );
		// // alert( r );
		// if ( !r ) {
			var SERVER_URL = waeparams.getContentProviderPersistence("mainhosturl");
			//	portalurl = SERVER_URL + "download/ktvv2.xml#portal/html/index.html";
			//portalurl = SERVER_URL + "download/ZIP_CONFIG.xml#portal/html/index.html";
			//portalurl = SERVER_URL + "download/ZIP_CONFIG_32.xml#portal_32/html/mainplus_pro.html";  //519
			//portalurl = SERVER_URL + "download/ZIP_CONFIG_29.xml#portal_29/html/mainplus.html";  //313
			portalurl = SERVER_URL + "download/ZIP_CONFIG_35.xml#portal_35/html/mainplus_v313.html";  //313v
			//portalurl = SERVER_URL + "download/ZIP_CONFIG_40.xml#portal_40/html/mainplus_tw.html";  //台湾
			portalurl = portalurl.replace("http://", "tva://" );
			portalurl = portalurl.replace("https://", "tvas://" );

			console.log("reload load url : " + portalurl );
			location.replace(  portalurl ) ;

			// var app = wae.create("Application");
			// app.restartApplication();
			return;
		// };

		// var intent = wae.create("Intent");
		// intent.startActivity("", "com.vatata.wae.localapp.LocalAppActivity");  
		// var app = wae.create("Application");
		// app.finishActivity();
		// return;
//	  intent.startActivity("com.android.tv.settings", "com.android.tv.settings.connectivity.NetworkActivity");  

	}
	
	var url = pakobj.getUpdateUrl();

	location.replace(  url ) ;
}

setInterval( function() {

 	if ( pakobj == null ) {
 		alert( "Error: fail to get app package info!" );
 		return;
 	}
	
 	if ( pakobj.getUpdateTime() > 1000 * timeout_sec ) {
// //		alert("connect server timeout!");
 		reload_page();
 	}

 	if ( !! pakobj.getUpdateError() ) {
// //		alert("Error: " + pakobj.getUpdateError() ); 
 		reload_page();
 	}
		
 }, 1000 * 5 );

// alert( 1 );

window.onWAEReady = uni_onready;
</script>

